#include <Windows.h>
//#include <processthreadsapi.h>
//#include <errhandlingapi.h>
//#include <synchapi.h>
#include <heapapi.h>
#include <cstdio>
#include <ctime>
#include <stdio.h>
// Global variable
CRITICAL_SECTION CriticalSection;

typedef struct consumer_s
{
    int num;
    int keyno;
    int iinterval;
} consumer, * pconsumer;

void time_count(int interval = 2)
{
    time_t now = time(0);
    struct tm time_obj;
    localtime_s(&time_obj, &now);
    int t2 = 0;


    printf("current time[%02d:%02d:%02d]\n", time_obj.tm_hour, time_obj.tm_min, time_obj.tm_sec);
    if ((time_obj.tm_min + interval) >= 60)
    {
        t2 = (time_obj.tm_hour + 1) < 24 ? time_obj.tm_hour + 1 : 0;
        printf("\n[%02d:%02d:%02d] would be comming\n", t2, (time_obj.tm_min + interval) - 60, time_obj.tm_sec);
    }
    else
    {
        printf("\n[%02d:%02d:%02d] would be comming\n", time_obj.tm_hour, time_obj.tm_min + interval, time_obj.tm_sec);
    }
}

void mykey_press_release_u(int keyno = 1)
{
    if (1 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x31, 0, 0, 0);
        keybd_event(0x31, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (2 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x32, 0, 0, 0);
        keybd_event(0x32, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (3 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x33, 0, 0, 0);
        keybd_event(0x33, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (4 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x34, 0, 0, 0);
        keybd_event(0x34, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (5 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x35, 0, 0, 0);
        keybd_event(0x35, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (6 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x36, 0, 0, 0);
        keybd_event(0x36, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (7 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x37, 0, 0, 0);
        keybd_event(0x37, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
    else if (8 == keyno)
    {
        keybd_event(0x11, 0, 0, 0);
        keybd_event(0x38, 0, 0, 0);
        keybd_event(0x38, 0, KEYEVENTF_KEYUP, 0);
        keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
    }
}

void getContentFromClipboared(HWND hData, HWND phw)
{
    //#[get content from clipboard]#
    OpenClipboard(phw);
    EmptyClipboard();

    SetClipboardData(CF_UNICODETEXT, hData);
    GetClipboardData(CF_UNICODETEXT);
    CloseClipboard();
    SendMessage(phw, WM_PASTE, 0, 0);
    Sleep(500);
    // Ctrl + V
    //keybd_event(0x11, 0, 0, 0);
    //keybd_event(0x56, 0, 0, 0);
    //keybd_event(0x56, 0, KEYEVENTF_KEYUP, 0);
    //keybd_event(0x11, 0, KEYEVENTF_KEYUP, 0);
}

void MyWndProc(int keyno = 1, int iinterval = 2)
{
    //LPPOINT pointOld;
    POINT point;
    LPWSTR pData;
    //wchar_t pData;
    wchar_t content[6];
    HWND phw, hData; //#hData;
    //const CString lis[2] = { "#¼ÓÓÍ", "#´ò¿¨" };
    //content = lis[0];

//	lstrcpy(content, L"打卡");
//	if (2 == keyno)
//	{
//		lstrcpy(content, L"#美滋滋");
//	}
//
    if (2 == keyno)
    {
        wcscpy(content, L"#美滋滋");
    }
    else
    {
        wcscpy(content, L"#打卡");
    }
    hData = (HWND)GlobalAlloc(GMEM_MOVEABLE, sizeof(content)+2);
    pData = (LPWSTR)GlobalLock(hData);
//	lstrcpy(pData, content);
    wcscpy(pData, content);

    point.x = 1347;
    point.y = 761;
    /*pointOld->x = 1347;
    pointOld->y = 761;*/
    //GetCursorPos(pointOld);
    mykey_press_release_u(keyno);
    //Sleep(1000);
    time_count(iinterval);
    phw = WindowFromPoint(point);
    Sleep(1500);
    ShowWindow(phw, SW_SHOW);
//	SetCursorPos(1695, 995);
    SetCursorPos(1347, 761);
    mouse_event(MOUSEEVENTF_LEFTDOWN, 1394, 764, 0, 0);
    mouse_event(MOUSEEVENTF_LEFTUP, 1394, 764, 0, 0);
    Sleep(1000);

	getContentFromClipboared(hData, phw);
//    if (2 == keyno)
//    {
//        SendMessage(phw, WM_CHAR, '#', 0);
//        keybd_event(0x55, 0, 0, 0);
//        keybd_event(0x55, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x49, 0, 0, 0);
//        keybd_event(0x49, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x49, 0, 0, 0);
//        keybd_event(0x49, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x55, 0, 0, 0);
//        keybd_event(0x55, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x32, 0, 0, 0);
//        keybd_event(0x32, 0, KEYEVENTF_KEYUP, 0);
//    }
//    else
//    {
//        SendMessage(phw, WM_CHAR, '#', 0);
//        keybd_event(0x52, 0, 0, 0);
//        keybd_event(0x52, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x53, 0, 0, 0);
//        keybd_event(0x53, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x48, 0, 0, 0);
//        keybd_event(0x48, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x48, 0, 0, 0);
//        keybd_event(0x48, 0, KEYEVENTF_KEYUP, 0);
//        keybd_event(0x31, 0, 0, 0);
//        keybd_event(0x31, 0, KEYEVENTF_KEYUP, 0);
//    }
    Sleep(500);
    //SendMessage(phw, WM_PASTE, 0, 0);
    // Enter Key Press
    keybd_event(VK_RETURN, 0, 0, 0);
    keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP, 0);
    Sleep(1000);
    //MessageBox(NULL, "MyWndProc", NULL, MB_OK);
    //printf("MyWndProc\n");

}

DWORD WINAPI consumerfun(LPVOID lpParam)
{
    consumer_s* consum;
    consum = (consumer_s*)lpParam;
    //for (size_t i = 0; i < consum->num; ++i)
    for (size_t i = 0; i < 50; ++i)
    {
        //Sleep(1000);
        //printf("----wait for producer running----\n");
        printf("cer-current thread name is: %d\n", GetCurrentThreadId());
        // Request ownership of the critical section.
        TryEnterCriticalSection(&CriticalSection);
        MyWndProc(consum->keyno, consum->iinterval);
        // Release ownership of the critical section.
        LeaveCriticalSection(&CriticalSection);
        Sleep(1000 * 60 * consum->iinterval + 2000);
    }
    return 0;
}

int main(int argc, char **argv)
{
    HANDLE Tri[7] = { 0 };
    DWORD ThreadID;
    pconsumer pconsum[7] = { 0 };
    printf("----start----\n");

    // Initialize the critical section one time only.
    if (!InitializeCriticalSectionAndSpinCount(&CriticalSection, 0x00000400))
        exit(1);
    for (size_t i = 0; i < 7; i++)
    {
        pconsum[i] = (pconsumer)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(consumer_s));
        if (pconsum[i] == NULL)
        {
            printf("Create consumer%d failed!: %d\n", i, GetLastError());
            exit(1);
        }
    }

    for (size_t i = 0; i < 7; i++)
    {
        if (0 == i || 6 == i)
        {
            pconsum[i]->keyno = i + 1;
            pconsum[i]->iinterval = 20;
        }
        else if (1 == i)
        {
            pconsum[i]->keyno = i + 1;
            pconsum[i]->iinterval = 35;
        }
        else if (i >= 2 && i != 6)
        {
            pconsum[i]->keyno = i + 1;
            pconsum[i]->iinterval = 30;
        }
    }

    for (size_t i = 0; i < 7; i++)
    {
        Tri[i] = CreateThread(NULL, 0, consumerfun, pconsum[i], 0, &ThreadID);
        Sleep(6000);
    }
    // Release resources used by the critical section object.
    DeleteCriticalSection(&CriticalSection);
    for (size_t i = 0; i < 7; i++)
    {
        WaitForSingleObject(Tri[i], INFINITE);
    }

    for (size_t i = 0; i < 7; i++)
    {
        CloseHandle(Tri[i]);
    }

    for (size_t i = 0; i < 7; i++)
    {
        HeapFree(GetProcessHeap(), 0, pconsum[i]);
    }

    /*HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, pconsum1);
    HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, pconsum2);
    HeapFree(GetProcessHeap(), HEAP_ZERO_MEMORY, pconsum3);*/

    delete pconsum;
    /*pconsum1 = NULL;
    pconsum2 = NULL;
    pconsum3 = NULL;*/
    //delete ThreadID;
    printf("Hello Window2\n");
    return 0;
}
